package com.higgins.eventtracking.client.health.components;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.dto.HealthAlertDTO;
import com.higgins.eventtracking.client.genericcomponents.GenericColumn;
import com.higgins.eventtracking.client.genericcomponents.GenericColumns;
import com.higgins.eventtracking.client.genericcomponents.GenericTable;

public class HealthAlertTable extends GenericTable	{
	private static final Logger log = Logger.getLogger(HealthAlertTable.class.getName());
	private final EventTrackingServiceAsync greetingService = GWT
			.create(EventTrackingService.class);

	public HealthAlertTable(GenericColumns genericColumns) {
		super(genericColumns);
		listsLoaded();
	}
	public void listsLoaded()	{
		retrieveDTOs();
	}
	public void retrieveDTOs()	{
		greetingService.retrieveHealthAlerts(new AsyncCallback<List<HealthAlertDTO>>()  {
			@Override
			public void onFailure(Throwable caught) {
				log.info("HealthAlertTable: " + caught.toString());
			}

			@Override
			public void onSuccess(List<HealthAlertDTO> result) {
				loadData(convertDTOtoRowArray(result));
			}
		});		
	}
	private List<List<Object>> convertDTOtoRowArray(List<HealthAlertDTO> healthAlertDTOs)	{
		List<List<Object>> rows = new ArrayList<List<Object>>();
		for (HealthAlertDTO healthAlertDTO:healthAlertDTOs)	{
			List<Object> row = new ArrayList<Object>();
			row.add(healthAlertDTO.getId());
			row.add(healthAlertDTO.getName()!=null?healthAlertDTO.getName():"");
			row.add(healthAlertDTO.getEventType()!=null?healthAlertDTO.getEventType():"");
			row.add(healthAlertDTO.getEventName()!=null?healthAlertDTO.getEventName():"");
			row.add(healthAlertDTO.getTimePeriod()!=null?healthAlertDTO.getTimePeriod():"");
			row.add(healthAlertDTO.getEvery()!=null?healthAlertDTO.getEvery():0);
			row.add(healthAlertDTO.getHourOfAlert()!=null?healthAlertDTO.getHourOfAlert():0);
			row.add(healthAlertDTO.getTab()!=null?healthAlertDTO.getTab():"");
			row.add(healthAlertDTO.getColumn()!=null?healthAlertDTO.getColumn():0);
			row.add(healthAlertDTO.getRow()!=null?healthAlertDTO.getRow():0);
			row.add(healthAlertDTO.getCount()!=null?healthAlertDTO.getCount():false);
			row.add(healthAlertDTO.getOnOff()!=null?healthAlertDTO.getOnOff():false);
			rows.add(row);
		}
		return rows;
	}
	protected void addObject(int row, int column, Object object)	{
		ensureRowAvailable(row);
		GenericColumn genericColumn = genericColumns.getColumn(column);
//		if (genericColumn.getType().equals(JobSiteDTO.class))	{
//			addJobSiteDTO(row, column, object, genericColumn);
	}
	
	public void updateItemCell(int row)	{
		HealthAlertDTO dto = new HealthAlertDTO();
		dto.setId(getId(row));
		dto.setName(getText(row, 1));
		dto.setEventType(getText(row, 2));
		dto.setEventName(getText(row, 3));
		dto.setTimePeriod(getText(row, 4));
		dto.setEvery(getInteger(row, 5));
		dto.setHourOfAlert(getDouble(row, 6));
		dto.setTab(getText(row, 7));
		dto.setColumn(getInteger(row, 8));
		dto.setRow(getInteger(row, 9));
		dto.setCount(getBoolean(row, 10));
		dto.setOnOff(getBoolean(row, 11));
		updateRow(dto);
	}
	private void updateRow(HealthAlertDTO dto)	{
		try {
			greetingService.updateHealthAlert(dto, new AsyncCallback<String>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("HealthAlertTable: " + caught.toString());
					}

					@Override
					public void onSuccess(String result) {
						setRowIdAfterUpdate(result);
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("HealthAlertTable: " + e.toString());
		} /*catch (SecurityAccessDeniedException e) {
			log.info("SecurityAccessDeniedException: " + e.toString());
		} 		*/
	}
}
